package com.bcx;


import com.bcx.pojo.Product;
import com.bcx.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@Api(tags = "处理Product请求")
public class ProductController {
    @Resource
    ProductService productService;
    /*查询全部*/
    @GetMapping("/selectAllProduct")
    @ApiOperation("查询所有product记录")
    public List<Product> selectAllProduct(){
        List<Product> products = productService.selectAllProduct();
        return products;
    }
    /*根据id查询*/
    @GetMapping("/selectProductById")
    @ApiOperation("根据id查询product")
    public Product selectProductById(Integer id){
        Product product = productService.selectProductById(id);
        return product;
    }
    /*修改*/
    @PostMapping("/updateProduct")
    @ApiOperation("根据id动态修改product")
    public Map<Object,Object> update(@RequestBody Product product){
        System.out.println(product.getId());
        Product product1 = productService.selectProductById(product.getId());
        product.setQuantity(product1.getQuantity()-product.getQuantity());
        int update = productService.update(product);
        Map<Object,Object> map=new HashMap<>();
        map.put("result",update);
        map.put("quantity",product.getQuantity());
        return  map;
    }

}
